// ActionScript file
import com.helloniu.fl.CoreComponentManager;
import com.helloniu.fl.NCHandler;
import com.helloniu.fl.NSHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.util.StyleTool;
import com.helloniu.fl.util.Tool;
import com.helloniu.red5.info.BussinessUnit;

private var ncHandler:NCHandler = null;
private var soHandler:SOHandler = null;

private var videoW:Number = 110;//110
private var videoH:Number = 110;//110
private var _userID:String;

private var publishStreamName:String = null;
private var publishHandler:NSHandler = null;
private var cam:Camera = null;
private var mic:Microphone = null;

public function initConnection(ancHandler:NCHandler, asoHandler:SOHandler, auserID:String):void {
	this.ncHandler = ancHandler;
	this.soHandler = asoHandler;
	this._userID = auserID;
    
	this.soHandler.addChangeListener(statusChange);
	setPublishStreamName(_userID + "avstream");
}

public function setPublishStreamName(apublishStreamName:String):void {
    publishStreamName = apublishStreamName;
}

private function statusChange(asoHandler:SOHandler, pname:String = null, 
	oldValue:* = null, newValue:* = null):void 
{
    if(pname == null) {
        return;
    }

    newValue = asoHandler.getProperty(pname);

	if(pname == "UpdateBussinessUnit") {
        //newValue
        //processUserListChange();
        var str:String = newValue + "";
    	var arr:Array = str.split(":");
    	updateUserInfo(arr[0], arr[1], arr[2]);        
    }
}

private function updateUserInfo(uid:String, key:String, 
	value:Object):void 
{
	CoreComponentManager.getIns().getComp("userList").updateUserInfo(uid, key, value == "true");
}

private function publishVideo(e:*):void {
	if(!publishVideoBtn.selected) {
    	if(publishHandler != null) {
        	publishHandler.closeStream();
        }

    	publishStreamChanged(false);
    	return;
    }
    
	cam = Camera.getCamera();
	mic = Microphone.getMicrophone();
    
	if(cam == null) {
    	Tool.traceT("Camera is null!");
    }

	if(mic == null) {
    	Tool.traceT("Microphone is null!");
    }

	Tool.initAV(mic, cam);

	if(cam != null || mic != null) {
    	publishHandler = new NSHandler(ncHandler.getNC());
     	publishHandler.setStreamName(publishStreamName);
     	publishHandler.setStartPublishFunc(startPublishFunc);
    	publishHandler.publish(mic, cam);
    }

	showLocalVideo();
}

private function showLocalVideo():void {
    // 本地视频大小
	var localVideo:Video = new Video();        
	localVideo.width = videoW;
	localVideo.height = videoH;
	localVideo.attachCamera(cam);            
	localVDShow.addChild(localVideo);
	localVDShow.width = videoW;
	localVDShow.height = videoH;
}

private function startPublishFunc(publishnsHandler:NSHandler):void {
	Tool.traceT(publishnsHandler.getStreamName() + ":publish OK");
    
	publishStreamChanged(true);
}

private function publishStreamChanged(published:Boolean):void {
    // @temp tonyy
    // update client
    // bytearray--base64--string
	soHandler.setProperty("UpdateBussinessUnit", 
    	_userID + ":" + "video" + ":" + published);
    
	updateBussinessUnit(published);
    
}

// update server side
private function updateBussinessUnit(published:Boolean):void {
	var bunit:BussinessUnit = new BussinessUnit();
	bunit.type = "video";
	bunit.opened = published;
    
	var barr:ByteArray = new ByteArray();
	bunit.writeData(barr);
    
	ncHandler.call("fl_ClientUpdate", null, barr);
}

private function initApp(e:*):void {
	StyleTool.setBtnStyle(publishVideoBtn, "openCameraIcon");
    StyleTool.setBtnSStyle(publishVideoBtn, "closeCameraIcon");
}